﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/BMP_TreeGrid.master"
    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Pages_TOU_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script type="text/javascript">

        ModelName = "TOUInfo";
        EmptyRow = [{ GID: "", PGID: "", Manager: "", Code: "", Name: "", OUType: "", state: "closed", Sequence: ""}]

        $(function () {
            $('#' + TreeGridTableID).treegrid({
                title: '组织机构管理',
                iconCls: 'icon-save',
                width: 650,
                height: 600,
                nowrap: false,
                rownumbers: true,
                animate: true,
                collapsible: true,
                url: vRoot + 'Services/Handler.ashx?Type=' + ModelName + '&PGID=0',
                idField: TreeIDField,
                treeField: 'Name',
                frozenColumns: [[]],
                columns: [[
					{ field: 'Name', title: '名称', width: 250 },
					{ field: 'Code', title: '编码', width: 300 },
                    { field: 'GID', title: '删除', width: 50, formatter: function (value) {
                        return '<a href="javascript:DeletebyID(\'' + value + '\');">删除</a>';
                    }
                    }
				]],
                onBeforeLoad: function (row, param) {
                    if (row) {
                        $(this).treegrid('options').url = vRoot + 'Services/Handler.ashx?Type=' + ModelName + '&PGID=' + row.GID;
                    } else {
                        $(this).treegrid('options').url = vRoot + 'Services/Handler.ashx?Type=' + ModelName + '&PGID=0';
                    }
                },
                onClickRow: function (row) {
                    OnRowClick(row);
                    OnRowClickInitRols(row);
                },
                onContextMenu: function (e, row) {
                    e.preventDefault();
                    $(this).treegrid('unselectAll');
                    $(this).treegrid('select', row[treeIDField]);
                    $('#menu').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                },
                onLoadSuccess: function () {
                    $('#editTable').show();
                },
                onLoadError: function (e) {
                    alert(e.responseText);
                }

            });
        });

        function OnRowClickInitRols(row) {
            GetRolsByOUAjaxMethod(row.GID, function (data) {
                SetRolesSelectData(data);
            });
        }
        function InsertTOU() {
            Insert(function (data) {
                data[0].state = "closed";
                return true;
            }, function (data, resultData) {
                SetRolsAjaxMethod(GetRolesFromSelect(), data[0].GID, function () {

                });
            }, function () {
            });
        }
        function ModifyTOU() {
            Modify(function (node) {
                node.state = "closed";
                return true;
            }, function (node, resultData) {
                node.state = "closed";
                SetRolsAjaxMethod(GetRolesFromSelect(), node.GID, function () {

                });
            }, function () {
            });
        }

        function GetRolesFromSelect() {
            var roles = $('#Roles');
            var ids = "";
            roles.each(function () {
                $(this).children("option:selected").each(function () {
                    ids += $(this).val() + ",";
                });
            });
            return ids;
        }

        function SetRolesSelectData(ids) {
            var idArray = ids.Trim(',').split(',');
            var roles = $('#Roles');
            roles.each(function () {
                $(this).children("option").each(function () {
                    if (ids.indexOf($(this).val()) >= 0)
                    { $(this)[0].selected = true; }
                    else
                    { $(this)[0].selected = false; }
                });
            });
        }

        function GetRolsByOUAjaxMethod(ouGID, myCallBack) {
            var postURI = vRoot + "Services/WebService.asmx/GetRolesByOU";
            var postData = "{ouGID: '" + ouGID + "'}";
            LSAjax(postURI, postData, myCallBack);
        }

        function SetRolsAjaxMethod(roleIDS, ouGID, myCallBack) {
            var postURI = vRoot + "Services/WebService.asmx/SaveOURoles";
            var postData = "{roleIDS: '" + roleIDS + "', ouGID: '" + ouGID + "'}";
            LSAjax(postURI, postData, myCallBack);
        }

        function LSAjax(postURI, postData, callBack) {
            $.ajax({
                url: postURI,
                type: "POST",
                data: postData,
                contentType: "application/json; charset=UTF-8",
                dataType: "json",
                success: function (data) {
                    if (typeof callBack != 'undefined')
                        callBack(data.d);
                },
                error: function (f) {
                }
            });
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contentPlaceHolder" runat="Server">
    <form runat="server" id="form1">
    <table>
        <tr style="vertical-align: top">
            <td>
                <table id="treeGridTable">
                </table>
            </td>
            <td>
                <table class="editTable" cellpadding="0" cellspacing="0" id="editTable" style="display: none">
                    <tr>
                        <th>
                            名称：
                        </th>
                        <td>
                            <input id="TOUInfo_Name" type="text" des="名称" emp="true" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            编号：
                        </th>
                        <td>
                            <input id="TOUInfo_Code" type="text" des="编号" emp="true" unq="true" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            序号：
                        </th>
                        <td>
                            <input id="TOUInfo_Sequence" type="text" des="序号" emp="true" exp="reg_Number" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            单位类型：
                        </th>
                        <td>
                            <input id="TOUInfo_OUType" type="text" des="单位类型" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            所属角色：
                        </th>
                        <td>
                            <asp:ListBox AutoPostBack="false" SelectionMode="Multiple" runat="server" ID="Roles"
                                ClientIDMode="Static"></asp:ListBox>
                        </td>
                    </tr>
                    <tr>
                        <th colspan="2" style="text-align: center; width: 100%;">
                            <input type="button" id="btnInsert" value="添加" onclick="InsertTOU();" style="width: 70px;" /><input
                                type="button" id="btnModify" value="修改" onclick="ModifyTOU();" style="width: 70px;" />
                        </th>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <div id="menu" class="easyui-menu" style="width: 120px;">
        <div onclick="Delete();">
            删除</div>
    </div>
    </form>
</asp:Content>
